<template>
  <div class="login">
    <div class="loginBox">
      <div class="avtor">
        <img src="https://ftp.bmp.ovh/imgs/2021/05/684c59686a76f389.jpg" alt />
      </div>
      <el-form
        label-width="80px"
        class="loginForm"
        ref="loginformref"
        :rules="rules"
        :model="formdata"
      >
        <el-form-item label="用户名" prop="username">
          <el-input
            prefix-icon="iconfont iconwode1"
            placeholder="请输入用户名"
            v-model="formdata.username"
          ></el-input>
        </el-form-item>
        <el-form-item label="密 码" prop="password">
          <el-input
            prefix-icon="iconfont iconpassword"
            placeholder="请输入密码"
            type="password"
            v-model="formdata.password"
          ></el-input>
        </el-form-item>
        <el-form-item class="buttons">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { log } from "util";
export default {
  name: "Home",
  props: [],
  components: {},
  data() {
    return {
      formdata: {
        username: "",
        password: ""
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 10, message: "长度在 3 到 5 个字符", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 6, max: 18, message: "长度在 6 到 18 个字符", trigger: "blur" }
        ]
      }
    };
  },
  //方法 函数写这里
  methods: {
    reset() {
      this.$refs.loginformref.resetFields();
    },
    login() {
      this.$refs.loginformref.validate(async valid => {
        if (!valid) {
        //   this.$message.error("登录失败 请重新输入!!!");
          return;
        }
        const res = await this.$http("/api/login", {
          params: this.formdata
        });
        console.log(res.data);
        if (res.data.code != 1) {
          return this.$message.error("登录失败 请重新输入!!!");
        }
        this.$message.success("登陆成功");
        window.sessionStorage.setItem("token", res.data.token);
        this.$router.push('/home')
      });
    }
  },
  //计算属性
  computed: {},
  //侦听器
  watch: {},
  //过滤器
  filters: {},
  //以下是生命周期
  //组件创建之前
  beforeCreate() {},
  //组件创建之后
  created() {},
  //页面渲染之前
  beforeMount() {},
  //页面渲染之后
  mounted() {},
  //页面视图数据更新之前
  beforeUpdate() {},
  //页面视图数据更新之后
  updated() {},
  //页面销毁之前
  beforeDestroy() {},
  //页面销毁之后
  destroyed() {}
};
</script>


<style  lang="scss"  scoped>
.login {
  background-color: darkcyan;
  height: 100vh;
}
.loginBox {
  width: 450px;
  height: 350px;
  background-color: #fff;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  border-radius: 5px;
}
.avtor {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  position: absolute;
  transform: translate(-15%, -15%);
  padding: 5px;
  box-shadow: 0 0 12px #dddddd;
  border: 1px solid #eee;
  background-color: #ffffff;
}
img {
  width: 60px;
  height: 60px;
  border-radius: 50%;
}
.loginForm {
  position: absolute;
  bottom: 50px;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}
.buttons {
  display: flex;
}
</style>